package com.acme.bankapp.domain.bank;

import java.util.ArrayList;
import java.util.List;

import com.acme.bankapp.service.bank.ClientRegistrationListener;


public class Bank {
	private final int MAX_CLIENTS = 10;
	private Client[] clients = new Client[MAX_CLIENTS];	
	private int clientCount = 0;
	private List<ClientRegistrationListener> listeners = new ArrayList<ClientRegistrationListener>();
	ClientRegistrationListener debugListener = new DebugListener();
	public Bank() {
		listeners.add(new ClientRegistrationListener() {
			
			@Override
			public void onClientAdded(Client c) {
				System.out.println("New client " + Client.getClientSalutation(c) + ", welcomoe!");
			}
		});
		listeners.add(new ClientRegistrationListener() {
			
			@Override
			public void onClientAdded(Client c) {
				System.out.println("Notification email for client: " + Client.getClientSalutation(c) + " to be sent");
			}
		});
		listeners.add(debugListener);
	}
	
	public class DebugListener implements ClientRegistrationListener{

		@Override
		public void onClientAdded(Client c) {
			System.out.println("Client " + Client.getClientSalutation(c) + " added on " + new java.util.Date());
		}
		
	}
	public void addClient(Client client) {
		if(this.clientCount >= 0 && this.clientCount < 10) {
			this.clients[this.clientCount] = client;
			this.incClientCount();
			for(ClientRegistrationListener l : this.listeners) {
				l.onClientAdded(client);
			}
		} else {
			System.out.println("Too many clients");
		}
	}
	
	public Client getClientById(int id) {
		if(this.getClientCount() > id && id >= 0) {
			return clients[id];
		} else {
			return null;
		}
	}

	public int getMaxClients() {
		return MAX_CLIENTS;
	}

	public int getClientCount() {
		return clientCount;
	}
	
	public void incClientCount() {
		this.clientCount++;
	}

}
